package DomainLayer;
import java.util.ArrayList;

public class SuppliersCollection
{
    private ArrayList<Supplier> suppliers;
    private static SuppliersCollection instance;
    
    private SuppliersCollection()
    {
        suppliers = new ArrayList<Supplier>();
    }
    
    //@return only one object of the class SuppliersCollection.
    public static SuppliersCollection getInstance(){
        if(instance==null){
            instance = new SuppliersCollection();
        }
        return instance;
    }
    
    public void addSupplier(Supplier supplier){
        suppliers.add(supplier);
    }
    
    public void deleteSupplier(Supplier supplier){
        suppliers.remove(supplier);
    }
    
    
    //@return supplier from the suppliers' list who has the product lowest price among others.
    public Supplier getSupplier(String productBarCode){
        double currentLowestPrice = 99999999;
        Supplier theSupplier = null;
        for(Supplier supplier:suppliers){
            if(supplier.getProduct().getBarCode().equals(productBarCode)&&supplier.getProduct().getPrice()<currentLowestPrice){
                currentLowestPrice=supplier.getProduct().getPrice();
                theSupplier = supplier;
            }
        }
        return theSupplier;
    }
}
